//中间件通用js

//得到某个人所有已教课程
function getPersonsTeachings(pid) {
	var url = 'person/json/get-persons-teachings.action?pid=' + pid;
	var result = null;
	$.getJSON(url, function(json) {
		
		if (!json.error) {
			result = json;
		} else {
			alert(json.error);
		}
	});
	return result;
};


//currentPage当前页.pageSize每页记录数
function getOtherPeoplesTeachings(type, currentPage, pageSize) {
	var url = 'course/json/want-study-search.action?search=' + type
			+ '&currentPage=' + currentPage + '&pageSize=' + pageSize;
	var result = "";
	$.getJSON(url, function(data) {
		result = data;
	});
	return result;
};


//将源推进客户端
function pushTeachingsArray(orig, dest) {
	$.each(orig, function(index, item) {
		var person = new Person({person : item.person});
		var course = new Course({course : item.course});
		var repeat = new Repeat({repeat : item.repeat});
		var teaching = new Teaching();
		teaching.person(person);
		teaching.course(course);
		teaching.repeat(repeat);
		dest.push(teaching);
	});
	
};


//得到某人的学习
function getPersonsStudies(pid) {
	var url = 'person/json/get-persons-studies.action?pid=' + pid;
	var result = null;
	$.getJSON(url, function(json) {
		
		if (!json.error) {
			result = json.studies;
		} else {
			alert(json.error);
		}
	});
	return result;
};

//将源数据推入目标数组中
function pushStudiesArray(orig, dest){
	$.each(orig, function(index, item) {
		var person = new Person({person : item.person});
		var course = new Course({course : item.course});
		var studying = new Studying();
		studying.id(item.id);
		studying.person(person);
		studying.course(course);
		dest.push(studying);
	});
};

function pushTimeTablesArray(orig, dest){
	$.each(orig, function(index, item) {
		var timeTable = new TimeTable();
		timeTable.id(item.id);
		timeTable.status(item.status);
		timeTable.content(item.content);
		if(item.teachDate.indexOf('T') != 0){
			item.teachDate = convertDateFromServerToClient(item.teachDate);
		}
		timeTable.teachDate(item.teachDate);
		dest.push(timeTable);
	});
}



function applyBindings(viewModel, viewNode){
	if(viewNode){
		ko.applyBindings(viewModel, viewNode);
	}else{
		ko.applyBindings(viewModel);
	}
}

function convertDateFromServerToClient(origDate){
	if(origDate.indexOf('T') != 0){
		var fullDate = origDate.split('T')[0];
		var fullTime = origDate.split('T')[1];
		
		var dateArray = fullDate.split('-');
		var timeArray  = fullTime.split(':');
		
		var destDate = new Date();
		destDate.setFullYear(dateArray[0], dateArray[1], dateArray[2]);
		destDate.setHours(timeArray[0],timeArray[1],timeArray[2]);
		return destDate;
	}
}




